package ee.ts022.tuum.services.ws.endpoints;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
import org.springframework.ws.server.endpoint.annotation.ResponsePayload;

import ee.ts022.tuum.domain.ContractStatusType;
import ee.ts022.tuum.domain.ContractType;
import ee.ts022.tuum.services.ClassifiersService;
import ee.ts022.tuum.services.ContractService;
import ee.ts022.tuum.services.CustomerService;
import ee.ts022.tuum.services.ws.generated.Contract;
import ee.ts022.tuum.services.ws.generated.SaveCustomerContract;
import ee.ts022.tuum.services.ws.generated.SaveCustomerContractResponse;
import ee.ts022.tuum.services.ws.utils.SoapDomainParser;

@Endpoint
public class ContractServiceEndpoint {

	public static final String NAMESPACE_URI = "http://ts022.ee";
	private static final Logger logger = LoggerFactory.getLogger(ContractServiceEndpoint.class);

	@Autowired
	ContractService contractService;
	@Autowired
	CustomerService customerService;
	@Autowired
	ClassifiersService classifiersService;
	 
	@PayloadRoot(localPart = "saveCustomerContract", namespace = NAMESPACE_URI)
	@ResponsePayload
	public SaveCustomerContractResponse saveCustomerContractResponse(@RequestPayload SaveCustomerContract saveCustomerContract) {
		logger.info("saving contract {} for customer {}", saveCustomerContract.getContract().getCustomer(), saveCustomerContract.getContract().getCustomer());
		
		boolean success = true;
		
		SaveCustomerContractResponse response = new SaveCustomerContractResponse();
		ee.ts022.tuum.domain.Contract contract = contractService.getContract(saveCustomerContract.getContract().getContract());
		if(contract != null) {
			Contract c = saveCustomerContract.getContract();
			contract.setCntNumber(c.getCntNumber());
			contract.setName(c.getName());
			contract.setDescription(c.getDescription());
			contract.setConditions(c.getConditions());
			contract.setNote(c.getNote());
			contract.setValueAmount(c.getValueAmount());
			try {
				contract.setValidFrom(SoapDomainParser.paresDate(c.getValidFrom()));
			} catch (Exception e) {
				logger.error("Kuupäeva parsimine ebaõnnestus: "+e.getMessage());
			}
			try {
				contract.setValidTo(SoapDomainParser.paresDate(c.getValidTo()));
			} catch (Exception e) {
				logger.error("Kuupäeva parsimine ebaõnnestus: "+e.getMessage());
			}
			ContractType type = classifiersService.getContractType(c.getContractType().getContractType());
			if(type != null){
				contract.setContractType(type);
			}
			ContractStatusType statusType = classifiersService.getContractStatusType(c.getContractStatusType().getContractStatusType());
			if(statusType != null){
				contract.setContractStatusType(statusType);
			}
			success = contractService.save(contract);
			
		} else {
			success = false;
		}
		
		if(!success) {
			logger.error("Lepingu salvestamine andmebaasi salvestamine ebaõnnestus");
			response.setReturnCode(1);
			response.setMessage("Lepingu salvestamine andmebaasi salvestamine ebaõnnestus");
		}
		
		return response;
	}
}
